﻿using Share;
using System.Collections.Generic;
using TableDef;

public class WordResData
{

    public string[] m_Lan = new string[(int)TypeDefine.ELanguageType.NUM];
    public string m_Voice;
    public void OnInit(t_language resData)
    {
        m_Lan[(int)TypeDefine.ELanguageType.CN] = resData.m_strCn;
        m_Lan[(int)TypeDefine.ELanguageType.US] = resData.m_strUs;
        m_Lan[(int)TypeDefine.ELanguageType.JP] = resData.m_strJp;
        m_Voice = resData.m_strVoice;
        //m_Lan[(int)TypeDefine.ELanguageType.US] = resData.m_strUs;
        //m_Lan[(int)TypeDefine.ELanguageType.JP] = resData.m_strJp;
        //m_Lan[(int)TypeDefine.ELanguageType.TH] = resData.m_strTh;
        //m_Lan[(int)TypeDefine.ELanguageType.MY] = resData.m_strMy;
        for (int i = 0; i < m_Lan.Length; ++i)
        {
            if (m_Lan[i] != null)
                m_Lan[i] = m_Lan[i].Replace("\\n", "\n");
            else
                m_Lan[i] = "";
        }
    }
}

public class LanguageManager : SingletonMonoObject<LanguageManager>
{

    TypeDefine.ELanguageType m_curLang = TypeDefine.ELanguageType.US;
    Dictionary<string, WordResData> m_dicWords = new Dictionary<string, WordResData>();


    public void SetCurLang(TypeDefine.ELanguageType lang)
    {
        m_curLang = lang;
    }

    public TypeDefine.ELanguageType GetCurLang()
    {
        return m_curLang;
    }
    public override void DoInit()
    {
        ResDataMgr<t_language>.Instance().DoAutoLoad("t_language");
        int nNum = ResDataMgr<t_language>.Instance().GetResDataNumber();
        m_dicWords.Clear();
        for (int i = 0; i < nNum; ++i)
        {
            var resData = ResDataMgr<t_language>.Instance().GetResDataByIndex(i);
            try
            {
                var data = new WordResData();
                data.OnInit(resData);
                m_dicWords.Add(resData.m_strName, data);
            }
            catch (System.Exception e)
            {
                XKLog.LogError("多语言表 t_language 发现重复key ={0}  id={1} ", resData.m_strName, resData.GetMainID());
            }
        }


    }

    public void DoReload()
    {
        ResDataMgr<t_language>.Instance().DoReload();
        DoInit();
    }
    public string GetVoice(string strKey)
    {
        if (string.IsNullOrEmpty(strKey))
        {
            return "";
        }
        WordResData resData;
        if (m_dicWords.TryGetValue(strKey, out resData))
        {
            if (resData.m_Voice == "")
            {
                XKLog.LogWarning($"多语言表里[{strKey}]没有填声音");
            }
            return resData.m_Voice;
        }
        XKLog.LogWarning("多语言表里没有   {0}   这个key", strKey);
        return "";
    }
    public string GetWord(string strKey)
    {
        if (string.IsNullOrEmpty(strKey))
        {
            //  XKLog.LogError("这个key异常 为空");
            return "";// strKey;
        }
        WordResData resData;
        if (m_dicWords.TryGetValue(strKey, out resData))
        {
            string result = ReplaceKeyOfString(resData.m_Lan[(int)m_curLang]);
            if (result == "")
            {
                return "多语言表里有key" + strKey + "但是没字";
            }
            return result;
        }
        XKLog.LogWarning("多语言表里没有   {0}   这个key", strKey);
        // 临时解决方案，写成中文了
        strKey = "sys_text_notext";
        if (m_dicWords.TryGetValue(strKey, out resData))
            return GetRealWord(strKey);
        return strKey;// strKey;
    }
    public string GetRealWord(string strKey)
    {
        WordResData resData;
        if (m_dicWords.TryGetValue(strKey, out resData))
            return resData.m_Lan[(int)m_curLang];
        XKLog.LogWarning("多语言表里没有   {0}   这个key", strKey);
        return strKey;
    }

    public string GetWord(string strKey, TypeDefine.ELanguageType lang)
    {
        if (string.IsNullOrEmpty(strKey))
        {
            XKLog.LogError("多语言key 为空");
            return "";
        }
        WordResData resData;
        if (m_dicWords.TryGetValue(strKey, out resData))
            return ReplaceKeyOfString(resData.m_Lan[(int)lang]);

        return strKey;
    }
    // 替换字符串中的关键字
    public static string ReplaceKeyOfString(string data)
    {
        if (string.IsNullOrEmpty(data))
            return "";


        //if (data.Contains("#player_name"))
        //{
        //    data = data.Replace("#player_name", DataPlayer.Instance.m_strName);
        //}
        //if (data.Contains("#player_gender"))
        //{
        //    string it = DataPlayer.Instance.m_nSex == 0 ? LanguageManager.Instance.GetWord("player_gender_she") : LanguageManager.Instance.GetWord("player_gender_he");
        //    data = data.Replace("#player_gender", it);
        //}

        //Dictionary<string, GenderResData> m_dicGenderWords = LanguageManager.Instance.DicGenderWords;
        //foreach (var item in m_dicGenderWords)
        //{
        //    if (data.Contains(item.Key))
        //    {
        //        string str = DataPlayer.Instance.m_nSex == 0 ? item.Value.m_she : item.Value.m_he;
        //        data = data.Replace(item.Key, LanguageManager.Instance.GetRealWord(str));
        //    }
        //}

        return data;
    }
}
